package com.yayan.beautyclub.service;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.yayan.beautyclub.bean.Category;
import com.yayan.beautyclub.dao.CategoryDao;
import com.yayan.beautyclub.dao.ICategoryDao;

@Service
public class CategoryService implements ICategoryService {

	private ICategoryDao categoryDao;
	
	public CategoryService(){
		categoryDao = new CategoryDao();
	}
	
	@Override
	public List<Category> getCategoryList() {
		// TODO Auto-generated method stub
		List<Category> list = null;
		try{
			list = categoryDao.getCategoryList();
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Category getCategory(int cId) {
		// TODO Auto-generated method stub
		Category category = null;
		try{
			if(cId != 0){
				category = categoryDao.getCategory(cId);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return category;
	}

	@Override
	public int saveCategory(Category category) {
		// TODO Auto-generated method stub
		int r = 0;
		try{
			if(category.getTime() == null)
				category.setTime(new Date());			
			r = categoryDao.saveCategory(category);
		}catch(Exception e){
			e.printStackTrace();
		}
		return r;
	}

	@Override
	public boolean updateCategory(Category category) {
		// TODO Auto-generated method stub
		boolean res = false;
		try{
			if(category.getcId() != 0L){
				if(category.getTime() == null){
					category.setTime(new Date());
				}
				res = categoryDao.update(category);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return res;
	}

	@Override
	public boolean deleteCategory(int cId) {
		// TODO Auto-generated method stub
		boolean res = false;
		try{
			if(cId != 0){
				res = categoryDao.delete(cId);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return res;
	}

	@Override
	public List<Category> getCategoryList(int parentId) {
		// TODO Auto-generated method stub
		List<Category> list = null;
		try{
			if(parentId >= 0)
				list = categoryDao.getCategoryList(parentId);
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

}
